package demo.supplier.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import demo.supplier.pojo.Goods;
import demo.supplier.pojo.GoodsType;
import demo.supplier.pojo.Supplier;
import demo.supplier.service.SupplierService;

@Controller
@RequestMapping("/supplier")
public class SupplierAction {

	@Autowired
	private SupplierService service;
	
	//查询所有供应商信息
	@RequestMapping("/supplier_list")
	public String supplier_list(Supplier supplier,Model model){
		List<Supplier> suppliers = service.supplier_list(supplier);
		model.addAttribute("suppliers", suppliers);
		return "/jsp/supplier/supplier.jsp";
	}
	//删除供应商
	@RequestMapping("/deleteSupplierById")
	public String deleteSupplierById(Long uuid){
		service.deleteSupplierById(uuid);
		return "redirect:/supplier/supplier_list.action";
	}
	//修改前查询方法
	@RequestMapping("/updateSupplierBefore")
	public String updateSupplierBefore(Long uuid,Model model){
		Supplier supplier = service.getSupplierById(uuid);
		model.addAttribute("supplier", supplier);
		return "/jsp/supplier/edit_supplier.jsp";
	}
	//修改供应商信息
	@RequestMapping("/editSupplier")
	public String editSupplier(Supplier supplier){
		service.editSupplier(supplier);
		return "redirect:/supplier/supplier_list.action";
	}
	//新建供应商信息
	@RequestMapping("/addSupplier")
	public String addSupplier(Supplier supplier){
		service.addSupplier(supplier);
		return "redirect:/supplier/supplier_list.action";
	}
	//商品类别查询
	@RequestMapping("/goodsType_list")
	public String goodsType_list(HttpServletRequest request,GoodsType Goods,Model model){
		List<GoodsType> goodsType = service.getGoodsTypeList(Goods);
		model.addAttribute("goodsType", goodsType);
		HttpSession session = request.getSession();
		List<Supplier> suppliers = service.supplier_lists();
		session.setAttribute("supp", suppliers);
		model.addAttribute("supp", suppliers);
		return "/jsp/supplier/goodsType.jsp";
	}
	//添加商品类别
	@RequestMapping("/addGoodsType")
	public String addGoodsType(GoodsType goodsType){
		service.addGoodsType(goodsType);
		return "redirect:/supplier/goodsType_list.action";
	}
	//删除商品类别
	@RequestMapping("/deleteGoodsTypeById")
	public String deleteGoodsTypeById(Long uuid){
		service.deleteGoodsTypeById(uuid);
		return "redirect:/supplier/goodsType_list.action";
	}
	//修改商品类别信息前
    @RequestMapping("/updateGoodsTypeBefore")
    public String updateGoodsTypeBefore(Long uuid,Model model){
    	GoodsType goodsType = service.updateGoodsTypeBefore(uuid);
    	model.addAttribute("goodsType", goodsType);
    	return "/jsp/supplier/edit_goodsType.jsp";
    }
  //修改商品类别信息
    @RequestMapping("/editGoodsType")
    public String editGoodsType(GoodsType goodsType){
    	service.editGoodsType(goodsType);
    	return "redirect:/supplier/goodsType_list.action";
    }
    //商品查询
    @RequestMapping("/goods_list")
    public String goods_list(Model model){
    	List<Goods> goods = service.goods_list();
    	model.addAttribute("goods", goods);
    	return "/jsp/supplier/goods.jsp";
    }
}
